package generics;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨宇
 * @version 1.0 2016/11/1
 */
public class E21_ClassTypeCapture {

    private Map<String, Class<?>> types = new HashMap<>();

    public void addType(String typename, Class<?> clazz) {
        types.put(typename, clazz);
    }

    public Object createNew(String typename) {
        Class<?> clazz = types.get(typename);

        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            System.out.println("error 1");
        } catch (IllegalAccessException e) {
            System.out.println("error 2");
        }
        return null;
    }

    public static void main(String[] args) {
        E21_ClassTypeCapture ctt = new E21_ClassTypeCapture();
        ctt.addType("Building", Building.class);
        ctt.addType("House", House.class);
        System.out.println(ctt.createNew("Building").getClass());
        System.out.println(ctt.createNew("House").getClass());
        ctt.createNew("Product");
    }
}

class Building {
}

class House extends Building {
}

